package ch9;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

public class DateClass {

	public static void main(String[] args) {

		//如何获得当前系统时间
		Date now = new Date();
		System.out.println(now.getTime());
		
		long time = System.currentTimeMillis();//unix - 在有些数据库设计中使用long类型保存时间
		now = new Date(time);
		System.out.println(time);
		
		
		//日历
		Calendar calendar = Calendar.getInstance();
		//年，月，日
		String dateString = new Integer(calendar.get(Calendar.YEAR)).toString();
		System.out.println(dateString);
		
		//使用calendar读取的月份是：0~11
		dateString = new Integer(calendar.get(Calendar.MONTH)).toString();
		System.out.println(dateString);
		
		dateString = new Integer(calendar.get(Calendar.DATE)).toString();
		System.out.println(dateString);
		
		//星期，星期是从星期天开始的，范围是1~7
		dateString = new Integer(calendar.get(Calendar.DAY_OF_WEEK)).toString();
		System.out.println(dateString);
		
		//时 分 秒
		//12小时进制：0~11
		dateString = new Integer(calendar.get(Calendar.HOUR)).toString();
		System.out.println(dateString);
		//24小时进制：0~23
		dateString = new Integer(calendar.get(Calendar.HOUR_OF_DAY)).toString();
		System.out.println(dateString);
		//分钟
		dateString = new Integer(calendar.get(Calendar.MINUTE)).toString();
		System.out.println(dateString);
		//秒
		dateString = new Integer(calendar.get(Calendar.SECOND)).toString();
		System.out.println(dateString);
		//毫秒
		dateString = new Integer(calendar.get(Calendar.MILLISECOND)).toString();
		System.out.println(dateString);

		//时间的计算
		//java.util.Date没有提供时间计算方法，时间一般使用Calendar进行计算
		//3天后
		calendar.set(Calendar.DATE, calendar.get(Calendar.DATE)+3);
		System.out.println(calendar.get(Calendar.DATE));
		//3个月以前
		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH)-3);
		System.out.println(calendar.get(Calendar.MONTH)+1);
		//5年以后
		calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR)+5);
		System.out.println(calendar.get(Calendar.YEAR));
		
		//时间与时间字符串间的转换
		//2018-8-7 15:15:15.234
		//2018年8月7日 15时15分15秒234毫秒
		//YYYY-年，MM-月，dd-日，HH-24小时，mm-分钟，ss-秒钟，SSS-毫秒
		String pattern ="yyyy-MM-dd HH:mm:ss.SSS";
		SimpleDateFormat sdf =new SimpleDateFormat(pattern);
		try {
			Date date =sdf.parse("2018-08-7 12:12:12.234");
			System.out.println(date);
			pattern="yyyy年MM月dd日 EE HH时mm分ss秒SSS毫秒";
			sdf.applyPattern(pattern);//修改时间格式
			
			String dateString2 =sdf.format(date);
			System.out.println(dateString2);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		
	}

}
